package com.zcsy.commons.util;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.zcsy.commons.model.MOPack;
import com.zcsy.commons.model.MTPack;
import com.zcsy.commons.model.StatusPack;





/**
 * @Title: XMLUtil.java
 * @Package： com.zcsy.web.websocket
 * @Description: 解析XML字符串
 * @author wangziliang 460694546@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年7月1日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
public class XMLUtil {

    private static final Logger logger = Logger.getLogger(XMLUtil.class);

    /**
     * @Title: parseMTPack
     * @Description: 将xml字符串装换为MTPack集合
     * @param xml
     *            xml字符串
     * @return
     */
    public static MTPack parseMTPack(String xml) {
        List<MTPack> list = parseMTPackList(xml);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * @Title: parseMTPackList
     * @Description: 将xml字符串装换为List<MTPack>集合
     * @param xml
     *            xml字符串
     * @return
     */
    public static List<MTPack> parseMTPackList(String xml) {
        List<MTPack> list = new ArrayList<MTPack>();
        if (xml.indexOf(":") != -1) {
            xml = xml.substring(xml.indexOf(":") + 1);
            Document doc = null;
            try {
                doc = DocumentHelper.parseText(xml); // 将字符串转为XML
                Element rootElt = doc.getRootElement(); // 获取根节点MTPacks
                Iterator<?> iters = rootElt.elementIterator("MTPack"); // 获取根节点下的子节点MTPack
                while (iters.hasNext()) {
                    Element recordEle = (Element)iters.next();
                    MTPack bean = new MTPack();
                    String id = recordEle.elementTextTrim("Id"); // 拿到sms节点下的子节点Id值
                    String mobile = recordEle.elementTextTrim("Mobile"); // 拿到sms节点下的子节点Mobile值
                    bean.setId(id);
                    bean.setMobile(mobile);
                    list.add(bean);
                }
            } catch (DocumentException e) {
                logger.error(e, e);
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        return list;
    }

    /**
     * @Title: parseStatusPackList
     * @Description: 将xml字符串装换为List<StatusPack>集合
     * @param xml
     *            xml字符串
     * @return
     */
    public static List<StatusPack> parseStatusPackList(String xml) {
        List<StatusPack> list = new ArrayList<StatusPack>();
        if (xml.indexOf(":") != -1) {
            xml = xml.substring(xml.indexOf(":") + 1);
            Document doc = null;
            try {
                doc = DocumentHelper.parseText(xml); // 将字符串转为XML
                Element rootElt = doc.getRootElement(); // 获取根节点StatusPacks
                Iterator<?> iters = rootElt.elementIterator("StatusPack"); // 获取根节点下的子节点StatusPack
                while (iters.hasNext()) {
                    Element recordEle = (Element)iters.next();
                    StatusPack bean = new StatusPack();
                    String id = recordEle.elementTextTrim("Id");
                    String status = recordEle.elementTextTrim("Status");
                    String errCode = recordEle.elementTextTrim("ErrCode");
                    bean.setId(id);
                    bean.setStatus(status);
                    bean.setErrCode(errCode);
                    list.add(bean);
                }
            } catch (DocumentException e) {
                logger.error(e, e);
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        return list;
    }

    /**
     * @Title: parseMOPackList
     * @Description: 将xml字符串装换为List<MOPack>集合
     * @param xml
     *            xml字符串
     * @return
     */
    public static List<MOPack> parseMOPackList(String xml) {
        List<MOPack> list = new ArrayList<MOPack>();
        if (xml.indexOf(":") != -1) {
            xml = xml.substring(xml.indexOf(":") + 1);
            Document doc = null;
            try {
                doc = DocumentHelper.parseText(xml); // 将字符串转为XML
                Element rootElt = doc.getRootElement(); // 获取根节点MOPacks
                Iterator<?> iters = rootElt.elementIterator("MOPack"); // 获取根节点下的子节点MOPack
                while (iters.hasNext()) {
                    Element recordEle = (Element)iters.next();
                    MOPack bean = new MOPack();
                    String destId = recordEle.elementTextTrim("DestId");
                    String mobile = recordEle.elementTextTrim("Mobile");
                    String message = recordEle.elementTextTrim("Message");
                    String moTime = recordEle.elementTextTrim("MOTime");
                    bean.setDestId(destId);
                    bean.setMobile(mobile);
                    bean.setMessage(message);
                    bean.setMoTime(moTime);
                    list.add(bean);
                }
            } catch (DocumentException e) {
                logger.error(e, e);
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String xml1 = "0:<MTPacks><MTPack><Id>951640</Id><Mobile>18771997644</Mobile></MTPack></MTPacks>";
        List<MTPack> mtPacks = parseMTPackList(xml1);
        for (int i = 0; i < mtPacks.size(); i++ ) {
            System.out.println(mtPacks.get(i).getId() + "-" + mtPacks.get(i).getMobile());
        }
        String xml2 = "0:<StatusPacks><StatusPack><Id>123</Id><Status>UNDELIV</Status><ErrCode>MK:0004</ErrCode></StatusPack><StatusPack><Id>124</Id><Status>DELIVRD</Status><ErrCode>DELIVRD</ErrCode></StatusPack></StatusPacks>";
        List<StatusPack> statusPacks = parseStatusPackList(xml2);
        for (int i = 0; i < statusPacks.size(); i++ ) {
            System.out.println(statusPacks.get(i).getId() + "-" + statusPacks.get(i).getStatus()
                               + "-" + statusPacks.get(i).getErrCode());
        }
        String xml3 = "0:<MOPacks><MOPack><DestId></DestId><Mobile></Mobile><Message></Message><MOTime></MOTime></MOPack><MOPack><DestId></DestId><Mobile></Mobile><Message></Message><MOTime></MOTime></MOPack></MOPacks>";
        List<MOPack> moPacks = parseMOPackList(xml3);
        for (int i = 0; i < moPacks.size(); i++ ) {
            System.out.println(moPacks.get(i).getDestId() + "-" + moPacks.get(i).getMobile() + "-"
                               + moPacks.get(i).getMessage() + "-" + moPacks.get(i).getMoTime());
        }
    }

}
